package com.zj.healthmanage.service.impl;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.pojo.Ordersetting;
import com.zj.healthmanage.mapper.OrdersettingMapper;
import com.zj.healthmanage.service.OrdersettingService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersettingServiceImpl implements OrdersettingService{

    @Resource
    private OrdersettingMapper ordersettingMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ordersettingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Ordersetting record) {
        return ordersettingMapper.insert(record);
    }

    @Override
    public int insertSelective(Ordersetting record) {
        return ordersettingMapper.insertSelective(record);
    }

    @Override
    public Ordersetting selectByPrimaryKey(Integer id) {
        return ordersettingMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Ordersetting record) {
        return ordersettingMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Ordersetting record) {
        return ordersettingMapper.updateByPrimaryKey(record);
    }

    @Override
    public void addOrderSetting(List<Ordersetting> ordersettingList) {
        if(ordersettingList != null && ordersettingList.size() > 0){
            for (Ordersetting ordersetting : ordersettingList) {
                //检查此数据（日期）是否存在
                int count = ordersettingMapper.findCountByOrderDate(ordersetting.getOrderdate());
                if(count > 0){
                    //已经存在，执行更新操作
                    ordersettingMapper.editNumberByOrderDate(ordersetting);
                }else{
                    //不存在，执行添加操作
                    ordersettingMapper.insertSelective(ordersetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<Ordersetting> ordersettingList = ordersettingMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (ordersettingList != null && ordersettingList.size() > 0){
            for (Ordersetting ordersetting : ordersettingList) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",ordersetting.getOrderdate().getDate());
                m.put("number",ordersetting.getNumber());
                m.put("reservations",ordersetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(Ordersetting ordersetting) {
        int count = ordersettingMapper.findCountByOrderDate(ordersetting.getOrderdate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            ordersettingMapper.editNumberByOrderDate(ordersetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            ordersettingMapper.insertSelective(ordersetting);
        }
    }

}
